package com.zixun.controller;

import com.zixun.model.Order;
import com.zixun.model.information.Information;
import com.zixun.model.request.Request;
import com.zixun.repository.OrderRepository;
import com.zixun.repository.request.RequestRepository;
import com.zixun.service.InformationService;
import com.zixun.service.OrderService;
import com.zixun.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by Basil on 16/8/29.
 */
@RestController
@RequestMapping("/api/order")
public class OrderWebApi {

    @Autowired
    private UserService userService;

    @Autowired
    private OrderService orderService;

    @Autowired
    private OrderRepository orderRepository;

    @Autowired
    private InformationService informationService;

    @Autowired
    private RequestRepository requestRepository;

    /**
     * @api {GET} /api/order/getOrderList 获取订单列表
     * @apiName getOrderList
     * @apiGroup request
     * @apiVersion 0.0.1
     * @apiDescription 获取订单列表
     * @apiParam {String} userId 用户编号
     * @apiParam {Integer} countPerPage 分页显示数量
     * @apiParam {Integer} pageIndex	当前页码
     * @apiSuccessExample 返回值: HTTP/1.1 200 OK
     * {
     * "returnValue":【1:成功, 2:参数错误,3:服务器异常,10:用户不存在, 整型】,
     * "errMsg":【错误信息, 字符串】.
     * "data":{
     * "orderNumber":【订单编号,字符串】
     * "createTime":【创建时间,长整型】,
     * "status":【订单状态,整型 0:已付款 0:未完成 1:已完成】,
     * "title":【资信标题,字符串】,
     * "isEvaluation":【0:未评价 1:已评价,整型】,
     * }
     * }
     */
    @RequestMapping("getOrderList")
    public Map<String,Object> getOrderList(String userId,Integer pageIndex,Integer countPerPage){
        Map<String,Object> result = new HashMap<>();
        try {
            if(userId==null||pageIndex==null||countPerPage==null){
                result.put("errMsg","参数错误");
                result.put("returnValue",2);
                return result;
            }
            if(!userService.isUserExist(Integer.valueOf(userId))){
                result.put("errMsg","用户不存在");
                result.put("returnValue",10);
                return result;
            }
            Page<Order> orderPage =orderService.findByUserIdPage(Integer.valueOf(userId),pageIndex,countPerPage);
            List<Map> orders = new ArrayList<>();
            for(Order order:orderPage){
                Map<String,Object> orderMap = new HashMap<>();
                orderMap.put("orderNumber",order.getOrderNumber());
                orderMap.put("createTime",order.getCreateTime().getTime());
                orderMap.put("status",order.getStatus());
                if(order.getIsEvaluation()==null){
                    order.setIsEvaluation(0);
                    orderRepository.save(order);
                }
                orderMap.put("isEvaluation",order.getIsEvaluation());
                if(order.getOrderType()==0){
                    Information information=informationService.findInformationByInformationId(order.getProduceId());
                    orderMap.put("title",information.getTitle());
                }
                if(order.getOrderType()==1){
                    Request request=requestRepository.findById(order.getProduceId());
                    orderMap.put("title",request.getTitle());
                }
                orders.add(orderMap);
            }
            result.put("errMsg","成功");
            result.put("returnValue",1);
            result.put("data",orders);
            return result;
        } catch (Exception e) {
            e.printStackTrace();
            result.put("errMsg","服务器异常");
            result.put("returuValue",3);
            return result;
        }
    }
}
